<?php
/**
 * Created by PhpStorm.
 * User: lzy
 * Date: 2018/11/7
 * Time: 9:51
 */

namespace App\FreeBird\Repositories;


use App\FreeBird\Contracts\CustomerServiceInterface;
use App\FreeBird\Models\CustomerServiceModel;
use App\Weixin\Models\WeixinUserModel;
use Illuminate\Support\Facades\Request;

class  DbCustomerServiceRepositories implements CustomerServiceInterface
{

    /**
     * 模型
     * @return CustomerServiceModel
     */
    public function getCustomerServiceModel()
    {
        return app()->make(CustomerServiceModel::class);
    }

    /**
     * 获取用户模型
     * @return WeixinUserModel
     */
    public function getWeixinUserModel(){
        return app()->make(WeixinUserModel::class);
    }

    /**
     * 列表
     * @param $where
     * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator|mixed
     */
    public function getList($where)
    {
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        return $this->getCustomerServiceModel()->newQuery()->where($where)->orderBy($sort, $order)->paginate($limit);
    }

    /**
     * 保存数据
     * @param $data
     * @return mixed|void
     */
    public function saveData($data)
    {
        if (empty($data['status'])) {
            $data['status'] = 0;
        }
        $data["uid"] = UID;
        return $this->getCustomerServiceModel()->newQuery()->updateOrCreate(["id" => $data['id']], $data);
    }

    /**
     * 添加用户客服数据
     * @param $data
     * @return mixed|void
     */
    public function addCustomerData($data)
    {
        $data['uid'] = AUID;
        return $this->getCustomerServiceModel()->newQuery()->create($data);
    }

    /**
     * 获取单条信息
     * @param $id
     * @return mixed|void
     */
    public function getInfo($id)
    {
        return $this->getCustomerServiceModel()->newQuery()->where("id", $id)->first();
    }

    /**
     * 删除数据
     * @param $where
     * @return mixed
     */
    public function deleteData($where)
    {
        return $this->getCustomerServiceModel()->newQuery()->where($where)->delete();
    }



    public function getListSerach($where){
        $request = Request::instance();
        $limit = empty($request->input("limit")) ? 10 : $request->input("limit");
        $sort = empty($request->input("sort")) ? 'create_time' : $request->input("sort");
        $order = empty($request->input("order")) ? 'desc' : $request->input("order");
        $username=$request->input("username");
        $where=function ($query) use ($username){
           $query->where($this->getWeixinUserModel()->table.".nickname","like",'%'.$username.'%');
           $query->where("buid",0);
        };
        return $this->getCustomerServiceModel()->newQuery()
            ->leftJoin($this->getWeixinUserModel()->table, $this->getWeixinUserModel()->table.'.uid', '=',  $this->getCustomerServiceModel()->table.'.uid')->where($where)->paginate($limit);
    }
    /**
     * 后台回复
     * @param $id
     * @param $msg
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|mixed
     */
    public function setAdminMessage($id,$msg){
        $data=array();
        $info=$this->getCustomerServiceModel()->newQuery()->where("id",$id)->first();
        $data["content"]=$msg;
        $data["uid"]=$info->uid;
        $data["buid"]=UID;
        $data["post_id"]=$id;
        return $this->getCustomerServiceModel()->newQuery()->create($data);
    }
}